package com.t01.bdqn.servlet;

import com.t01.bdqn.entity.ComputerRoom;
import com.t01.bdqn.entity.dto.ComputerRoomQuery;
import com.t01.bdqn.entity.dto.LayuiPage;
import com.t01.bdqn.service.ComputerRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;


// 前端访问的地址是：  context_path/springMvcController/test/hello
@Controller
@RequestMapping(value = "/springMvcController")

public class SpringMvcController {


    public SpringMvcController() {
        System.out.println("创建SpringMvcController");
    }

    @Autowired
    ComputerRoomService computerRoomService;

    @RequestMapping(value = "/test/hello")
    @ResponseBody
    public String hello() throws SQLException, IOException, ClassNotFoundException {
        System.out.println();
        return "hellospringmv2c";
    }

    @RequestMapping(value = "/hello2")
    @ResponseBody//返回json格式数据
    public List<ComputerRoom> hello2() throws SQLException, IOException, ClassNotFoundException {
        List<ComputerRoom> computerRoomList =  computerRoomService.ComputerSet(1,10);
        return computerRoomList;
    }


    //返回jsp页面
    @RequestMapping(value = "/hello3")
    public String hello3(){
        return "student";
    }

    //返回ModelAndView
    @RequestMapping(value = "/hello4")
    public ModelAndView hello4(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name","杨浩彬");
        modelAndView.setViewName("hello4");
        return modelAndView;
    }

    //接受参数
    @RequestMapping(value = "/hello5")
    @ResponseBody//返回json格式数据
    public List<ComputerRoom> hello5(int pageNo,int count) throws SQLException, IOException, ClassNotFoundException {
        System.out.println(pageNo);
        List<ComputerRoom> computerRoomList =  computerRoomService.ComputerSet(pageNo,count);
        return computerRoomList;
    }

    //接受 对象  --》 只能接收到 与对象属性名一样的参数
    @RequestMapping(value = "/hello6")
    @ResponseBody//返回json格式数据
    public List<ComputerRoom> hello6(ComputerRoomQuery computerRoomQuery) throws SQLException, IOException, ClassNotFoundException {
        System.out.println(computerRoomQuery);
        List<ComputerRoom> computerRoomList =  computerRoomService.ComputerSet(computerRoomQuery.getStart(),computerRoomQuery.getLimit());
        return computerRoomList;
    }


    //封装返回数据格式
    @RequestMapping(value = "/hello7")
    @ResponseBody//返回json格式数据
    public LayuiPage<ComputerRoom> hello7(ComputerRoomQuery computerRoomQuery) throws SQLException, IOException, ClassNotFoundException {
        LayuiPage<ComputerRoom> layuiPage = new LayuiPage();
        layuiPage.setCode(0);
        layuiPage.setMsg("查询成功");
        layuiPage.setData(computerRoomService.ComputerSet(computerRoomQuery));
        layuiPage.setCount(computerRoomService.ComputerSetCount(computerRoomQuery));
        return layuiPage;
    }



}
